/**
 * @author wangj881
 * @date 2025-01-17 17:20
 */
public class L2_3 {

    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

            return addTwo(l1, l2, 0);
        }

        private ListNode addTwo(ListNode l1, ListNode l2, int carry) {
            if (l1 == null && l2 == null) {
                return carry == 1 ? new ListNode(1) : null;
            }

            if (l1 == null) {
                l1 = l2;
                l2 = null;
            }

            int val = l1.val + carry + (l2 == null ? 0 : l2.val);
            l1.val = val % 10;

            l1.next = addTwo(l1.next, l2 == null ? null : l2.next, val / 10);

            return l1;
        }
    }

}
